package com.whcoding.properties.controller;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.lang.Dict;
import com.whcoding.properties.property.ApplicationProperty;
import com.whcoding.properties.property.DeveloperProperty;
import com.whcoding.properties.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Properties;


@RestController
@RequestMapping("property/")
public class PropertyController {
	private final ApplicationProperty applicationProperty;
	private final DeveloperProperty developerProperty;

	/**
	 * 获取环境的配置信新
	 */
	@Value("${spring.profiles.active}")
	private String activeName;

	/**
	 * http://localhost:10022/property/activeName
	 * <p>
	 * 获取当前环境信息
	 *
	 * @return
	 */
	@GetMapping("activeName")
	public String getCurrentEnvironment() {
		System.out.println("当前使用的环境是" + activeName);
		return activeName;
	}

	@Autowired
	public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) {
		this.applicationProperty = applicationProperty;
		this.developerProperty = developerProperty;
	}

	/**
	 * 1.获取当前环境的配置信息
	 * http://localhost:10022/property/property
	 *
	 * @return
	 */
	@GetMapping("/property")
	public Dict index() {
		return Dict
				.create()
				.set("applicationProperty", applicationProperty)
				.set("developerProperty", developerProperty);
	}

	/**
	 * http://localhost:10022/property/getPropertyStr
	 * <p>
	 * 获取 Property 内容
	 *
	 * @return
	 */
	@GetMapping("/getPropertyStr")
	public String getPropertyStr() {
		String propertyName = "application-temp.properties";
		Properties properties = PropertiesUtil.getProperties(propertyName);

		System.out.println("读取application-temp.properties 配置文件信息:" + properties);
		String serverPortValue = PropertiesUtil.getValue("server.port", propertyName);
		System.out.println("读取指定配置server.port 数据为: " + serverPortValue);
		//更新whcoding 的值为whcoding2
		PropertiesUtil.setValue("whcoding", "whcoding2", propertyName);
		String whcodingValue = PropertiesUtil.getValue("whcoding", propertyName);
		System.out.println("读取更新后的 whcoding 参数的值:" + whcodingValue);
		return whcodingValue;
	}

	/**
	 * 读取配置文件
	 *
	 * @return
	 */
	@GetMapping("queryApplicationSetting")
	public String queryApplicationSetting(@RequestParam String applicationName, @RequestParam String applicationNode) {
		Properties properties = PropertiesUtil.getProperties(applicationName);
		System.out.println("applicationName 配置文件信息:" + properties);
		//根据配置文件中key获取value
		String nodeValue = properties.getProperty(applicationNode);
		//根据配置文件 信息以及key 获取value
		String nodeValue2 = PropertiesUtil.getValue(applicationNode, applicationName);
		String resultStr = resultStr(applicationName, nodeValue, nodeValue2);
		String resultStr2 = resultStr(applicationName, applicationNode, nodeValue2);
		System.out.println(resultStr + ":" + resultStr2);
		return resultStr2;
	}


	private String resultStr(String str1, String str2, String str3) {
		return String.format("%s %s %s", str1, str2, str3);
	}

	/**
	 * 使用hutool读取配置文件
	 *
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/classPath")
	public String classPathByHutool() throws IOException {
		//获取定义在src/main/resources文件夹中的配置文件
		ClassPathResource resource = new ClassPathResource("application-temp.properties");
		Properties properties = new Properties();
		properties.load(resource.getStream());
		System.out.println(properties);
		return properties.toString();
	}

}
